﻿var PageNumber = 1;
var IsSearchView = false;
var QuotaIDView = 0;
var QuotaComElem = null;
var QuotaComElemVal = null;
var QuotaTimerId = null;
$(document).ready(function () {
    var path = window.location.pathname;
    if (path.indexOf("SetupManageQuotation") != -1) {
        LoadManageQuotationGrid();
    }
});
$(document).on('change', '.ChangedManageQuotationElements', function () {
    $('#ManageQuotationDetailsSaveBtn').addClass('BackColorRed');
});
$(document).on('click', '#ManageQuotationMoreBtn', function () {
    PageNumber = PageNumber + 1;
    LoadManageQuotationGrid();
});
$(document).on('click', '#ManageQuotationSearchBtn', function () {
    IsSearchView = true;
    LoadManageQuotationGrid();
});
$(document).on('click', '#ManageQuotationDetailsSaveBtn', function () {
    var $form = $('#LoadManageQuotationDetailsForm');
    if ($form.valid()) {
        if ($form[0].length > 0) {
            $.ajax({
                url: $form.attr('action'),
                type: $form.attr('method'),
                data: $form.serialize(),
                beforeSend: function (xhr) {
                    xhr.overrideMimeType('text/plain; charset=x-user-defined');
                },
                success: function (data) {
                    if (data.indexOf("Error") != -1)
                        jAlert(data, "Error");
                },
                error: function (xhr) {
                    jAlert(xhr.statusText, "Error");
                },
                complete: function () {
                    $('#ManageQuotationDetailsSaveBtn').removeClass('BackColorRed')
                    jAlert("Data SuccessFully Saved:", "Successful");
                    LoadManageQuotationGrid();
                }
            });
        }
        else
            jAlert("You have not data to save:", "Attention!");
    }
});
function LoadManageQuotationGrid() {
    var SetupManageQuotationsGridViewModel = {
        QuotaIDText: $("#QuotaIDText").val(),
        QuotaCodeText: $("#QuotaCodeText").val(),
        QuotaNameText: $("#QuotaNameText").val(),
        PageNum: parseInt(PageNumber),
        IsSearch: IsSearchView,
        QuotaID: parseInt(QuotaIDView),
        IsConfirmedSearch: $("#IsConfirmedSearch").val(),
        IsActiveSearch: $("#IsActiveSearch").val(),
    };

    var DTO = JSON.stringify(SetupManageQuotationsGridViewModel);
    $.ajax({
        url: urls["SetupLoadManageQuotationsGrid"],
        type: 'POST',
        contentType: 'application/json; charset=utf-8',
        data: DTO,
        success: function (data) {
            $("#SetupManageQuotationGrid").html(data);
        },
        error: function (xhr) {
            jAlert(xhr.statusText, "Error");
        },
        complete: function () {
        }
    });
}
// text element complete text 
$(document).on('input', '.QuotaTxtComplete', function () {
    if (QuotaTimerId == null) {
        QuotaComElem = $(this).attr('id');
        QuotaComElemVal = $("#" + QuotaComElem).val();
        QuotaTimerId = setInterval(function () {

            var keyword = $("#" + QuotaComElem).val();
            if (keyword == QuotaComElemVal) {
                var PropName = "";
                if (QuotaComElem.indexOf("QuotaCodeText") != -1)
                    PropName = "QuotaCodeText";
                if (QuotaComElem.indexOf("QuotaNameText") != -1)
                    PropName = "QuotaNameText";

                var position = $("#" + QuotaComElem).offset();
                position.top = position.top + $("#" + QuotaComElem).height() + 5;
                $.ajax({
                    url: urls["SetupAutoComplete"],
                    data: { 'PropName': PropName, 'keyword': keyword },
                    type: 'POST',
                    success: function (data) {
                        $("#AutoCompleteDiv").html(data);
                        var width = $("#" + QuotaComElem).css("width");
                        $("#AutoCompleteDiv").css("width", width);
                        $("#AutoCompleteDiv").show();
                        $("#AutoCompleteDiv").offset(position);
                    },
                    error: function (xhr) {
                        jAlert(xhr.statusText, "Error");
                    },
                    complete: function () {
                        clearInterval(QuotaTimerId);
                        QuotaTimerId = null;
                    }
                });
            }
            else
                QuotaComElemVal = keyword;
        }, 600);
    }
});
$(document).on('click', '.QuotaAutoCompleteDivClose', function () {
    $(this).parent().parent().hide();
});
function htmlDecode(value) {
    return $('<div/>').html(value).text();
}
function AutoCompleteRowClick(key, val) {
    val = htmlDecode(val);
    $("#" + QuotaComElem).val(val);
    if (QuotaComElem.indexOf("QuotaCodeText") != -1)
        $("#" + QuotaComElem).next().val(key);
    if (QuotaComElem.indexOf("QuotaNameText") != -1)
        $("#" + QuotaComElem).next().val(key);
    $("#AutoCompleteDiv").hide();
}